package com.anxinr.controller.user;

import com.anxinr.pojo.dto.AddressDTO;
import com.anxinr.pojo.entity.Address;
import com.anxinr.result.Result;
import com.anxinr.service.AddressService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收货地址
 *
 * @author anxinr
 */
@CrossOrigin
@RestController
@RequestMapping("/addr")
@Api("收货地址")
@Slf4j
public class AddrController {
    @Autowired
    private AddressService addressService ;

    /**
     * 保存收货地址
     * @param addr
     * @return
     */
    @PostMapping("/save")
    public Result<String> saveAddr(@RequestBody AddressDTO addr){
        addressService.saveAddr(addr) ;
        return Result.success() ;
    }

    /**
     * 设置默认收货地址
     * @param id
     * @return
     */
    @GetMapping("/defaultAddr")
    public Result<String> defaultAddr(@RequestParam("id") Long id){
        addressService.defaultAddr(id) ;
        return Result.success() ;
    }

    /**
     * 删除收货地址
     * @param id
     * @return
     */
    @GetMapping("/remove")
    public Result<String> remove(@RequestParam("id") Long id){
        addressService.removeById(id) ;
        return Result.success() ;
    }

    /**
     * 收货地址查询
     * @param
     * @return
     */
    @GetMapping("/query")
    public Result<List<Address>> queryAddr(){
        return new Result(addressService.queryAddress()) ;
    }

    /**
     * 收货修改
     * @param
     * @return
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody Address address){
        addressService.updateById(address) ;
        return Result.success() ;
    }
}
